import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2022-12-16
 * Time:19:24
 */
//封装关于博客表的相关操作
//     create table blog (
//             ->     blogId int primary key auto_increment,
//             ->     title varchar(256),
//             ->     content text,
//             ->     postTime datetime,
//             ->     userId int
//             -> );
public class BlogDao {

    //1、插入一个博克到数据库中--发布博客
    public void insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //1、建立数据库连接
            connection =  DBUtil.getConnection();
            //2、构造sql
            String sql = "insert into blog values(null,?,?,now(),?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getBlogId());
            //3、执行sql
            int ret = statement.executeUpdate();
            if(ret!=1) {
                System.out.println("博客插入失败");
            } else{
                System.out.println("博客插入成功！");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //4、释放资源
            DBUtil.close(connection,statement,null);
        }


    }

    //2、根据博客id来查询指定博客--博客详情页
    public Blog selectOne(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //1、建立连接
            connection = DBUtil.getConnection();
            //2、构造sql
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            //3、执行sql
            resultSet = statement.executeQuery();

            //4、遍历结果集合
            if(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }

        return null;
    }

    //3、直接查询博客列表--博客列表页
    public List<Blog> selectAll() {
        //存放
        List<Blog> blogs = new ArrayList<>();

        Connection connection = null;
        PreparedStatement  statement = null;
        ResultSet resultSet = null;


        try {
            //1、建立数据库连接
            connection = DBUtil.getConnection();
            //2、构造sql
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);

            //3、执行sql
            resultSet = statement.executeQuery();

            while(resultSet.next()) {
                Blog  blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                //避免博客列表页显示的内容太长了
                if(content.length()>100) {
                    content = content.substring(0,100)+"...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }

        return blogs;
    }


    //4、删除指定博客--删除博客
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;


        try {
            //1、建立数据库连接
            connection = DBUtil.getConnection();
            //2、构造sql
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            //3、执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                System.out.println("博客删除成功！");
            } else {
                System.out.println("博客删除失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,null);
        }

    }

}
